//
//  UIColor+ZUtilities.m
//
//  Created by Zed Link on 1/8/2016.
//  Copyright © 2016 itHeima. All rights reserved.
//

#import "UIColor+ZUtilities.h"

@implementation UIColor (ZUtilities)

+ (instancetype)zRandomColor
{
    return [self zColorWithRed:arc4random_uniform(256) green:arc4random_uniform(256) blue:arc4random_uniform(256)];
}

+ (instancetype)zColorWithHex:(uint32_t)hex
{
    uint8_t red = (hex & 0xff0000) >> 16;
    uint8_t green = (hex & 0x00ff00) >> 8;
    uint8_t blue = (hex & 0x0000ff);
    
    return [self zColorWithRed:red green:green blue:blue];
}

+ (instancetype)zColorWithRed:(uint8_t)red green:(uint8_t)green blue:(uint8_t)blue
{
    return [self colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
}

- (NSData *)changeData {
    
    int colorValue=[UIColor changeUIColorToRGB:self];
    
    unsigned char buf[4];
    //buf[0]=4;
    buf[0]='A';
    buf[1]=(colorValue/0x10000)&0xff;
    buf[2]=(colorValue/0x100)&0xff;
    buf[3]=colorValue&0xff;
    NSData *data = [NSData dataWithBytes: buf   length:4];
    
    return data;
}

// 返回颜色RBG值
+ (NSArray *)colorForGGB:(UIColor *)color {
    
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    return @[@(components[0] * 255.0),@(components[1] * 255.0),@(components[2] * 255.0)];
}

//将UIColor转换为RGB值
+ (int) changeUIColorToRGB:(UIColor *)color
{
    int value=0;
    //获得RGB值描述
    NSString *RGBValue = [NSString stringWithFormat:@"%@",color];
    //将RGB值描述分隔成字符串
    NSArray *RGBArr = [RGBValue componentsSeparatedByString:@" "];
    //获取红色值
    
    int r = [[RGBArr objectAtIndex:1] floatValue] * 255;
    
    value=value<<8;
    value+=r;
    //获取绿色值
    int g = [[RGBArr objectAtIndex:2] floatValue] * 255;
    
    value=value<<8;
    value+=g;
    //获取蓝色值
    int b = [[RGBArr objectAtIndex:3] floatValue] * 255;
    
    value=value<<8;
    value+=b;
    //返回保存RGB值的数组
    
    return value ;
}


@end
